package com.example.questionnaire.controller;

import com.example.questionnaire.common.ServerResponse;
import com.example.questionnaire.exception.RException;
import com.example.questionnaire.service.AppidAndSecretService;
import com.example.questionnaire.service.QuestionTitleService;
import com.example.questionnaire.service.UserAnswerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.util.List;



@RestController
@RequestMapping("/UserAnswer")
@Api(tags = "用户答题")
public class UserAnswerController {

    @Autowired
    private UserAnswerService userAnswerService;
    @Autowired
    AppidAndSecretService appidAndSecretService;

    /**
     *
     * @param formid（问卷id）
     * @param answerSource（答案来源）
     * @param ip（ip）
     * @param phone（答题者手机号）
     * @param openid（答题者微信id）
     * @param result（答题数据）
     * @param score（用户答卷分数）
     * @return
     * @throws ParseException
     */

    @PostMapping("/saveUserAnswerData")
    @ApiOperation(value = "存用户答题数据", response = ServerResponse.class, httpMethod = "POST")
    @Transactional(rollbackFor = {Exception.class, RuntimeException.class, RException.class})
    public ServerResponse saveUserAnswerData(@RequestParam String formid,
                                                  @RequestParam String answerSource,
                                                  @RequestParam String ip,
                                                  @RequestParam String phone,
                                                  @RequestParam String openid,
                                                  @RequestParam String result,
                                                  @RequestParam int score) throws ParseException {
//        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
//        String token = request.getHeader("token");
//
//        if(appidAndSecretService.tokenTimeout(token)){
//            String msg = "登录已过期";
//            return ServerResponse.createBySuccess(10010,msg, null);
//
//        }
        boolean b = userAnswerService.saveUserAnswerData(formid,answerSource,ip,phone,openid,result,score);
        if (b){
            return ServerResponse.createBySuccess("保存成功");
        }
        return ServerResponse.createByError("保存失败");
    }

    /**
     *
     * @param formid（问卷id）
     * @return
     * @throws ParseException
     * @throws IOException
     */
    @GetMapping("/statistics")
    @ApiOperation(value = "统计数据", response = ServerResponse.class, httpMethod = "GET")
    @Transactional(rollbackFor = {Exception.class, RuntimeException.class, RException.class})
    public ServerResponse statistics(@RequestParam String formid) throws ParseException, IOException {

        return ServerResponse.createBySuccess(userAnswerService.statistics(formid));

    }

    /**
     *
     * @param formid（问卷id）
     * @param ip（ip）
     * @param phone（答题者手机号）
     * @param openid（答题者微信id）
     * @param type（0为查询全部时间的答题情况，1为查询当天的答题情况）
     * @return
     * @throws ParseException
     * @throws IOException
     */
    @PostMapping("/limitValidation")
    @ApiOperation(value = "限制验证", response = ServerResponse.class, httpMethod = "POST")
    @Transactional(rollbackFor = {Exception.class, RuntimeException.class, RException.class})
    public ServerResponse limitValidation(@RequestParam String formid,@RequestParam String ip, @RequestParam String phone, @RequestParam String openid, @RequestParam int type) throws ParseException, IOException {

        return ServerResponse.createBySuccess(userAnswerService.limitValidation(formid,ip,phone,openid,type));

    }

    /**
     *
     * @param formid（问卷id）
     * @return
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    @GetMapping("/Statement")
    @ApiOperation(value = "详细数据", response = ServerResponse.class, httpMethod = "GET")
    @Transactional(rollbackFor = {Exception.class, RuntimeException.class, RException.class})
    public ServerResponse dataStatement(@RequestParam String formid) throws InvocationTargetException, IllegalAccessException{


        List<Object> b = userAnswerService.getStatement(formid);
        if (b != null){
            return ServerResponse.createBySuccess(b);
        }
        return ServerResponse.createByError("获取失败");
    }


}
